package com.yl.applicationsecurity.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.pojo.ApplicationCoverage;
import com.yl.applicationsecurity.pojo.Inspection;
import com.yl.applicationsecurity.pojo.Result;
import com.yl.applicationsecurity.service.ApplicationCoverageService;
import com.yl.applicationsecurity.service.ComponentUnauthorizedService;
import com.yl.applicationsecurity.utils.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-01-26 10:05:08
 */
@RestController
@RequestMapping("/applicationCoverage")
public class ApplicationCoverageController {

    @Autowired
    private ApplicationCoverageService applicationCoverageService;


    @RequestMapping("/list")
    public Result list(ApplicationCoverage applicationCoverage,@RequestParam(defaultValue = "1", value = "pageNum")Integer pageNum
            ,@RequestParam(defaultValue = "10", value = "pageSize")Integer pageSize ){

        PageInfo<ApplicationCoverage> applicationCoverageList=null;

        applicationCoverageList =  applicationCoverageService.findListByApplicationCoverage(applicationCoverage,pageNum,pageSize);

        return Result.success(applicationCoverageList);

    }


    @RequestMapping("/getStatisticalData")
    public Result getStatisticalDdata( ){


        Map<String,Integer> map = applicationCoverageService.getStatisticalDdata();



        return Result.success(map);

    }


    @RequestMapping("/getData")
    public Result getData() throws Exception {
        applicationCoverageService.getData();
        return Result.success("数据获取完成");
    }





    // 调试代码：保存生成的 Excel 文件到本地
@GetMapping("/export")
public ResponseEntity<byte[]> exportExcel(HttpServletResponse response) throws IOException {

    List<ApplicationCoverage> list =  applicationCoverageService.list();

    // 表头
    List<String> headers = Arrays.asList("应用名称", "是否为互联网应用", "RASP是否覆盖", "交互式逻辑探针是否覆盖","IAST是否覆盖");

    // 数据列映射
    List<String> columns = Arrays.asList("appName", "isInternet", "raspStatus", "logicProbeStatus", "iastStatus");

    // 调用工具类进行导出
    ByteArrayOutputStream excelStream = ExcelExportUtil.exportToApplicationCoverageExcel(list, headers, columns);

    HttpHeaders headersResponse = new HttpHeaders();
    headersResponse.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=探针覆盖情况.xlsx");



    // 返回Excel文件（作为二进制流）
    byte[] excelFile = excelStream.toByteArray();

    HttpHeaders headers2 = new HttpHeaders();
    headersResponse.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=探针覆盖情况.xlsx");

    return ResponseEntity.ok()
            .headers(headers2)
            .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
            .body(excelFile);
}





}

